package com.fll.fangyiuser.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fll.fangyicommon.entity.ResultOk;
import com.fll.fangyicommon.entity.user.Message;
import com.fll.fangyicommon.entity.user.User;
import com.fll.fangyicommon.vo.Chat;
import com.fll.fangyiuser.service.MessageService;
import com.fll.fangyiuser.service.FriendService;

import com.fll.fangyiuser.service.UserService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;


/**
 * @author fll
 * create 2023/3/3
 */
@RequestMapping("/message")
@RestController
public class MessageController {


    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @Autowired
    private FriendService friendService;


    //历史消息
    @GetMapping("/getHistoryMessage")
    public ResultOk getHistoryMessage(@RequestParam("friendId") Integer friendId, @RequestHeader String userInfo){
        ResultOk<ArrayList<Message>> ok = new ResultOk<>();

        if (friendId == null){
            ok.setCode(400);
            ok.setMsg("friendId不能为空！");
            return ok;
        }

        User user = null;;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            ArrayList<Message> messages = messageService.getHisMsg(user.getUserId(), friendId);
            if (messages != null){
                ok.setCode(200);
                ok.setMsg("获取历史消息成功");
                ok.setData(messages);
            }else{
                ok.setCode(400);
                ok.setMsg("历史消息为空");
            }
        } catch (IOException e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端错误");
            return ok;
        }
        return ok;
    }

    @GetMapping("/getChat")
    public ResultOk getChat(@RequestHeader String userInfo){
        ResultOk<List<Chat>> ok = new ResultOk<>();
        User user = null;
        List<Chat> chats = new ArrayList<>();
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            HashSet<Integer> chatSet = messageService.getChat(user.getUserId());
            if (chatSet != null) {
                //遍历好友friendId
                for (Integer friendId : chatSet) {
                    Chat chat = new Chat();
                    //1 查询我发给friendId的或friendId发给我的最新一条消息(内容和时间)
                    Message theNewOne = messageService.getTheNewOne(user.getUserId(), friendId);
                    chat.setNewMsg(theNewOne.getMessageContent());
                    chat.setSendTime(theNewOne.getSendTime());

                    chat.setUserId(friendId);
                    //2 查询出好友头像和个性签名
                    User userAndOssByUserId = userService.getUserAndOssByUserId(friendId);
                    if (userAndOssByUserId != null) {
                        chat.setNickName(userAndOssByUserId.getNickName());
                        chat.setUserAvatar(userAndOssByUserId.getOss().getUserAvatarUrl());
                    }
                    chats.add(chat);
                }

                ok.setCode(200);
                ok.setMsg("获取成功");
                ok.setData(chats);
                return ok;
            }
            ok.setCode(400);
            ok.setMsg("列表为空");
            return ok;
        } catch (IOException e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端错误");
            return ok;
        }
    }

}
